import React,{Component} from 'react';
import {Icon,Button} from 'antd';
import {Redirect} from 'react-router-dom';
import axios from 'axios';
import './index.scss';
class Center extends Component{
    constructor(){
        super();
        this.state = {
            tokenType:1
        }
    }
    //对于是否已经登录的判断我们放在componentWillMount
	componentDidMount(){
		//token的验证
		axios({
			method:'get',
			url:'http://192.168.2.251:7001/center',
			headers:{
				'Authorization':localStorage['token']
			}
		}).then((res)=>{
			// console.log(res);
			if(res.data.code === 0){//登录状态获取成功
				this.setState({
					tokenType:true
				})
			}else{
				//this.props.history.push('/login');
				this.setState({
					tokenType:false
				})
			}
			// console.log(this.state.tokenType);
		})
		
		
	}
    backFunccenter=()=>{
        this.props.history.push('/')
    }
    //退出
    logout=()=>{
        // console.log("jdhsfkasjf")
        localStorage['token'] = "";
		this.props.history.push('/lognin');
    }
    componentWillUnmount(){
		this.setState = (state,callback)=>{
			return;
		}
	}
    render(){
        if(this.state.tokenType){
            return (
                <div id='center'>               
                    <div className='center_top'><div className="div1" onClick={this.backFunccenter}><Icon type="left" className="leftBack" /></div><div className="div2">用户中心</div><Button type="danger" className='div3' onClick={this.logout} size='small'>退出</Button></div>
                    <div className='center_content'>
                        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546858655350&di=e5916939857dc6c30b496621ebc6d172&imgtype=0&src=http%3A%2F%2Ffiles.57gif.com%2Fwebgif%2F0%2Fa%2F80%2F48f0db6e6de9e028298e1cbdae7ae.gif" alt='dd'/>
                        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546858933864&di=3fe1cfb8098998295716ce815704f2a3&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F683a86865d2b68aa180720355d20539c152b3aaa7ec0a-DV2vEa_fw658" alt="dd"/>
                    </div>
                </div>
            )
        }else{
            return <Redirect to='/lognin' />
        }
    }
}
export default Center;